import com.troyworks.particleengine.PhysicsParticle;
import flash.filters.GlowFilter;
import com.troyworks.framework.SelectionManager;
import flash.filters.DropShadowFilter;
/**
 * @author Troy Gardner
 */
class com.troyworks.framework.ui.DraggableClip extends MovieClip {
	public var isBeingDragged : Boolean = false;
	private var m_isSelected : Boolean = false;
	private var m_selectedOrder : Number = 0;
	public static var selectionMan:SelectionManager;
	var filtersArr : Array;
	var glow : GlowFilter = null;

	private var lastMX : Number;

	private var lastMY : Number;

	public var lastX : Number;

	public var lastY : Number;
	public var id : Number;
	public var p : PhysicsParticle;
	public var label_txt:TextField;
	
	public static var MOVING:Number= 0;
	public static var COPYING:Number= 1;
	
	public var dragMode:Number= MOVING;

	private var tY:Number= null;

	private var tX :Number= null;
	
	public function DraggableClip() {
		super();
		trace("DraggableClip");
		if(selectionMan == null){
			selectionMan = new SelectionManager("DraggableClipSelectionManager");
		}
	}
	
	public function onLoad() : Void{
	//	onReleaseOutside =onRelease;
		label_txt.text = String(id);
	}

	public function get isSelected():Boolean{
		return m_isSelected;
	}

	public function set isSelected(t : Boolean){
		m_isSelected = t;
		onSelectionChanged();
	}
	
	public function onPress() : Void{
		trace("selected " + _name);
	
		isSelected = !isSelected;
					tX = null;
			tY = null;
	}
	
	public function onRelease():Void{
		selectionMan.onChildRelease = selectedOrder;
		if(tX != null && tY != null){
			_x = tX;
			_y = tY;
			tX = null;
			tY = null;
			isSelected = false;
		}
	}
	
	public function onSelectionChanged() : Void{
		if(isSelected){
			selectionMan.push(this);
			onStartDrag();			
		}else{
			selectionMan.remove(this);
			selectedOrder = -1;
			onStopDrag();
		}
	}
	
	public function set selectedOrder(pos:Number){
		m_selectedOrder = pos;
		trace("selected Order " + pos + "--------------------------------------");
		filtersArr = new Array();
		if(isSelected){
			selectionMan.setTopClip(this);
			 if (m_selectedOrder == (selectionMan.totalSelected)){
				glow = new GlowFilter(0xFF0000,100, 2,2,10, 10);
			}else if(m_selectedOrder == 1){
				glow = new GlowFilter(0x00FF00,100, 2,2,10, 10);
	
			}else{
				glow = new GlowFilter(0xFFFF00,100, 2,2,10, 10);
			}
				var ds:DropShadowFilter = new DropShadowFilter(10,45,0xCCCCCC,4);
				ds.alpha = 4;
				ds.blurX = 10;
				ds.blurY = 10;
				
			filtersArr.push(glow);
			filtersArr.push(ds);
		}else{
		}
		filters = filtersArr;
		
	}
	
	public function get selectedOrder():Number{
		return m_selectedOrder;
	}

	
	public function onStartDrag():Void{
		if(isSelected){
			if(dragMode == MOVING){
				lastMX = _parent._xmouse;
				lastMY = _parent._ymouse;
				onMouseMove = drag;
				isBeingDragged = true;
				lastX = _x;
				lastY = _y;
			}else if(dragMode == COPYING){
				_parent.myBitmapData.draw(this);
			}
		}
	}
	
	public function onStopDrag():Void{
		delete onMouseMove;
		isBeingDragged = false;
	}
	
	public function drag() : Void{	
		trace(_name + " MouseMOve/Drag");
		_x += _parent._xmouse - lastMX;
		_y += _parent._ymouse- lastMY;
		lastMX = _parent._xmouse;
		lastMY = _parent._ymouse;

	}
}